#include <stdio.h>

/*
:SA,-53.37,-28.88,250.44
:TS,00010100000135,35.0,+25.0,   0.0,1500.0,003
:BI,    -3,   -91,   +26,    +0,A
:BS,    -3,   -91,   +26,A
:BE,   +75,   -45,   -37,A
:BD,       +0.01,       -0.01,       -0.01,  +0.20,  0.19
*/

int main() {

    // 综合应用示例

    char input_str[] = {":SA,-53.37,-28.88,250.44\r\n :TS,00010100000135,35.0,+25.0,   0.0,1500.0,003\r\n :BI,    -3,   -91,   +26,    +0,A\r\n :BS,    -3,   -91,   +26,A\r\n :BE,   +75,   -45,   -37,A\r\n \
        :BD,       +0.01,       -0.01,       -0.01,  +0.20,  0.19\r\n"};

    int str_len = sizeof(input_str) / sizeof(input_str[0]);
    printf("input_str length: %d\n", str_len);

    char r_bytes;
    int r_count = 0;
    char r_buff[300] = {0};

    for (int i = 0; i < str_len; i++) {
        r_bytes = input_str[i];

        if (r_bytes == ':') {
            r_buff[r_count] = r_bytes;
            r_count++;
        } else if (r_count != 0) {
            r_buff[r_count] = r_bytes;
            r_count++;
        }

        if (r_bytes == '\n') {
            r_buff[r_count] = '\0';
            printf("r_buff: %s\n", r_buff);

            float value_east_dis, value_north_dis, value_up_dis, value_device_bottom_dis, value_effective_times;
            if (sscanf(r_buff, ":BD,%f,%f,%f,%f,%f\r\n", 
                &value_east_dis, &value_north_dis, &value_up_dis, &value_device_bottom_dis, &value_effective_times) == 5) {
                printf("Parsed BD message:\n");
                printf("value_east_dis(m): %f\n", value_east_dis);
                printf("value_north_dis(m): %f\n", value_north_dis);
                printf("value_up_dis(m): %f\n", value_up_dis);
                printf("value_device_bottom_dis(m): %f\n", value_device_bottom_dis);
                printf("value_effective_times: %f\n", value_effective_times);
            }

            float value_pitch_angle, value_roll_angle, value_yaw_angle;
            if (sscanf(r_buff, ":SA,%f,%f,%f\r\n", 
                &value_pitch_angle, &value_roll_angle, &value_yaw_angle) == 3) {
                printf("Parsed SA message:\n");
                printf("value_pitch_angle(degree): %f\n", value_pitch_angle);
                printf("value_roll_angle(degree): %f\n", value_roll_angle);
                printf("value_yaw_angle(degree): %f\n", value_yaw_angle);
            }

            r_count = 0;
        }

        if (r_count >= 300) {
            r_count = 0;
        }
    }

    // 综合应用示例  ↑

    printf("---------------\r\n");

    char input_BD_data_str[] = {":BD,       +0.01,       -0.01,       -0.01,  +0.20,  0.19\r\n"};

    float value_east_dis, value_north_dis, value_up_dis, value_device_bottom_dis, value_effective_times;
    if (sscanf(input_BD_data_str, ":BD,%f,%f,%f,%f,%f\r\n", 
        &value_east_dis, &value_north_dis, &value_up_dis, &value_device_bottom_dis, &value_effective_times) == 5) {
        printf("Parsed BD message:\n");
        printf("value_east_dis(m): %f\n", value_east_dis);
        printf("value_north_dis(m): %f\n", value_north_dis);
        printf("value_up_dis(m): %f\n", value_up_dis);
        printf("value_device_bottom_dis(m): %f\n", value_device_bottom_dis);
        printf("value_effective_times: %f\n", value_effective_times);
    }

    printf("---------------\r\n");

    char input_BE_data_str[] = {":BE,   +75,   -45,   -37,A\r\n"};

    int value_east_velocity, value_north_velocity, value_up_velocity;
    if (sscanf(input_BE_data_str, ":BE,%d,%d,%d,A\r\n", 
        &value_east_velocity, &value_north_velocity, &value_up_velocity) == 3) {
        printf("Parsed BE message:\n");
        printf("value_east_velocity(mm/s): %d\n", value_east_velocity);
        printf("value_north_velocity(mm/s): %d\n", value_north_velocity);
        printf("value_up_velocity(mm/s): %d\n", value_up_velocity);
    }

    printf("---------------\r\n");

    char input_BS_data_str[] = {":BS,    -3,   -91,   +26,A\r\n"};

    int value_ship_forward_x_velocity, value_ship_right_y_velocity, value_ship_down_z_velocity;
    if (sscanf(input_BS_data_str, ":BS,%d,%d,%d,A\r\n", 
        &value_ship_forward_x_velocity, &value_ship_right_y_velocity, &value_ship_down_z_velocity) == 3) {
        printf("Parsed BS message:\n");
        printf("value_ship_forward_x_velocity(mm/s): %d\n", value_ship_forward_x_velocity);
        printf("value_ship_right_y_velocity(mm/s): %d\n", value_ship_right_y_velocity);
        printf("value_ship_down_z_velocity(mm/s): %d\n", value_ship_down_z_velocity);
    }

    printf("---------------\r\n");

    char input_BI_data_str[] = {":BI,    -3,   -91,   +26,    +0,A\r\n"};

    int value_forward_x_velocity, value_right_y_velocity, value_down_z_velocity, value_velocity_err;
    if (sscanf(input_BI_data_str, ":BI,%d,%d,%d,%d,A\r\n", 
        &value_forward_x_velocity, &value_right_y_velocity, &value_down_z_velocity, &value_velocity_err) == 4) {
        printf("Parsed BI message:\n");
        printf("value_forward_x_velocity(mm/s): %d\n", value_forward_x_velocity);
        printf("value_right_y_velocity(mm/s): %d\n", value_right_y_velocity);
        printf("value_down_z_velocity(mm/s): %d\n", value_down_z_velocity);
        printf("value_velocity_err(mm/s): %d\n", value_velocity_err);
    }

    printf("---------------\r\n");

    char input_SA_data_str[] = {":SA,-53.37,-28.88,250.44\r\n"};

    float value_pitch_angle, value_roll_angle, value_yaw_angle;
    if (sscanf(input_SA_data_str, ":SA,%f,%f,%f\r\n", 
        &value_pitch_angle, &value_roll_angle, &value_yaw_angle) == 3) {
        printf("Parsed SA message:\n");
        printf("value_pitch_angle(degree): %f\n", value_pitch_angle);
        printf("value_roll_angle(degree): %f\n", value_roll_angle);
        printf("value_yaw_angle(degree): %f\n", value_yaw_angle);
    }

    printf("---------------\r\n");

    return 0;
}